/*
 * Copyright (c) 2011, Andrea Palloni
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * - Neither the name of the copyright holder nor the names of its contributors may be used
 * 	  to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.palla.biblio.server.service;

import java.util.ArrayList;
import java.util.List;

import net.palla.biblio.server.dao.CddDao;
import net.palla.biblio.server.entity.CddEntity;
import net.palla.biblio.shared.Cdd;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Represents a service for managing Cdd entities
 * 
 * @author andrea.palloni
 * 
 */
public class CddService {

	private CddDao dao = new CddDao();

	/**
	 * Converts a CddEntity to Cdd
	 * @param cdd
	 * @return
	 */
	private Cdd convertFromEntity(CddEntity cdd) {
		Cdd c = new Cdd();
		c.setId(KeyFactory.keyToString(cdd.getId()));
		c.setCdd1(cdd.getCdd1());
		c.setCdd2(cdd.getCdd2());
		c.setName(cdd.getName());
		if (cdd.getParent() != null) {
			c.setParent(KeyFactory.keyToString(cdd.getParent()));
		}		
		return c;
	}

	/**
	 * Converts a Cdd to CddEntity
	 * 
	 * @param cdd
	 * @return
	 */
	private CddEntity convertToEntity(Cdd cdd) {
		CddEntity c = new CddEntity();
		if (cdd.getId() != null) {
			Key key = KeyFactory.stringToKey(cdd.getId());
			c.setId(key);
		}
		c.setCdd1(cdd.getCdd1());
		c.setCdd2(cdd.getCdd2());
		c.setName(cdd.getName());
		if (cdd.getParent() != null) {
			c.setParent(KeyFactory.stringToKey(cdd.getParent()));
		}		
		return c;
	}

	/**
	 * Deletes an entity
	 * @param author
	 * @throws Exception
	 */
	public void delete(Cdd cdd) throws Exception {
		Key key = KeyFactory.stringToKey(cdd.getId());
		CddEntity c = (CddEntity)dao.getById(key);
		dao.delete(c);
	}

	/**
	 * Gets a List of Cdd
	 * @param cdd1 first part of the Cdd key
	 * @param cdd2 second part of Cdd Key (can be null)
	 * @return
	 */
	public List<Cdd> getByCdd(String cdd1, String cdd2) {
		List<CddEntity> res = dao.getByCdd(cdd1, cdd2);
		List<Cdd> response = new ArrayList<Cdd>();
		for (CddEntity e : res) {
			response.add(convertFromEntity(e));
		}
		return response;
	}
	
	/**
	 * Gets a cdd by key
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Cdd getById(String id) throws Exception {
		Key key = KeyFactory.stringToKey(id);
		CddEntity auth = (CddEntity) dao.getById(key);
		return convertFromEntity(auth);
	}
	
	/**
	 * Saves or updates an entity
	 * @param cdd
	 * @return
	 * @throws Exception
	 */
	public Cdd saveOrUpdate(Cdd cdd) throws Exception {
		CddEntity c = convertToEntity(cdd);
		c = (CddEntity) dao.saveOrUpdate(c);
		return convertFromEntity(c);
	}

	public List<Cdd> getByParent(String parentkey) {
		List<CddEntity> res = null;
		if (parentkey != null) {
			Key key = KeyFactory.stringToKey(parentkey);
			res = dao.getByParent(key);
		} else {
			res = dao.getByParent(null);
		}
		List<Cdd> response = new ArrayList<Cdd>();
		for (CddEntity e : res) {
			response.add(convertFromEntity(e));
		}
		return response;
	}
}
